    <?php 
   
    function singleGenealogy($parent_level, $level = 0, $levelIds, &$selection_count)  {
    	?>
    	
    	<?php 
    	if ($parent_level != null){
	       $children = Doctrine_Core::getTable('Level')->getLevelChildren($parent_level->getId());
	       if(count($children) > 0) {
	        	$level++;
		       	foreach ($children as $child) {
		       		$children_count = Doctrine_Core::getTable('Level')->countLevelChildren($parent_level->getId());
		       		$class = $children_count > 0 ? "jstree-open" : "jstree-leaf jstree-last";
		       		?>
		       		<li id="<?php echo $child->getId();?>" class = "<?php echo $class; ?>" >
		       		<a href="#">
		       		<?php $checked = $child->isMemberOfSelection($levelIds) ? "checked = 'checked'" : "";
		       		if ($checked != ""){
		       			$selection_count++;
		       		}
		       		?>
		       		<?php 
		       		
		             echo '<input '. $checked.' class="ignore" type="checkbox" name="check_'. $child->getId().'" id="check_'. $child->getId().'" />' . '<span class="tree_caption">' . $child->getName() . '</span>';
		             
		             ?>
		             </a>
		             <ul id="ul_<?php echo $child->getId()?>_<?php echo $level?>">
			             <?php 
			             singleGenealogy($child, $level,$levelIds, $selection_count);
			             ?>
		             </ul>
		             </li>
		             <?php 
		       	}
	       }
    	}
       ?>
       <?php 
    }
    
    ?>
       	<div class="rowElem noborder">
       	
     		<label for="search_title">Search title:</label>
     		<div class="formRight">
     		
     		<?php 
     		$selected_directory_obj = Doctrine_Core::getTable('MasterDirectory')->find($selected_directory);
     		?>
     		
     		<input type="text" id="search_title" style="width: 233px" name="search_title" value="<?php echo $selected_directory_obj->getSearchTitle(); ?>" />
     		</div>       
     		
     		<label for="search_title">Search button caption:</label>
     		<div class="formRight">
     		
     		<input type="text" id="search_button_caption" style="width: 233px" name="search_button_caption" value="<?php echo $selected_directory_obj->getSearchButtonCaption(); ?>" />
     		</div>  
     		
     		<label for="search_title" style="width: 215px;">Include Search Template in Sitemap?</label>
     		<div class="formRight">
     		
     		<?php $include_sitemap_checked = $selected_directory_obj->getSearchInSitemap()? "checked = 'checked'" : ""; ?>
     		<input type="checkbox" id="include_in_sitemap" name="include_in_sitemap" value="1" <?php echo $include_sitemap_checked; ?> />
     		</div>  
     		
     		<div style="clear: both;"></div>		
       	
	     	<label for="directory_type">Template:</label>
     		<div class="formRight" >
                    			<select name="template_id" id="template_id" onchange="loadSearchConfiguration()" style="min-width:240px;"  >
                    			
                    				<?php 
                    					
                    					foreach ($templates as $template) {
                    						
                    						
	                    					if (count($template->getMasterDirectory()) == 0){
	                    						$selected = $template->getId() == $selected_template_id ? "selected" : "";
	                    					?>
	                    						<option <?php echo $selected ?>  value="<?php echo $template->getId(); ?>"><?php echo $template->getName(); ?></option>
	                    					<?php 
                    						}
                    					}
                    				?>
                            	</select>     		
     		
     		</div>
     		<div style="clear:both">

     		</div>
     		<div class="fix"></div>
     	</div> 
  
    <div style="clear:both">
		<div class="our_subtitle" style="float:left">
			Involved levels
		</div>
		<div class="our_dashed" style="float: left; height: 1px; margin-top: 10px; width: 690px;">
		
		</div>
	</div>
	<div style="clear: both; width: 824px;">
			<?php 
			
			$i = 0;
			foreach ($categories as $category) {
					$i++;
					$root_level = Doctrine_Core::getTable('Level')->getRootLevel($category->getId());
		       		?>
		       		
		       		<div style="float:left; margin-left: 20px;margin-top: 20px">
				    <div class="head">
				    	<h5 class="iList"><?php echo $category->getName()?> Levels Tree</h5>
				    </div>
				    
				    <?php if ($root_level != null) {?>
				    <div  id="tree_<?php echo $category->getId()?>" class="demo">
				    	<ul id="ul_<?php echo $category->getId()?>_0">
				    		<li id="<?php echo $root_level->getId();?>" class="jstree-open">
								<?php $checked = $root_level->isMemberOfSelection($levelIds) ? "checked = 'checked'" : "" ?>
								<a href="#"><?php echo '<input type="checkbox" ' . $checked . '  class="ignore"  name="check_'. $root_level->getId().'" id="check_'. $root_level->getId().'" />' .'<span class="tree_caption">' . $root_level->getName(); ?></span></a>
								<ul id="ul_<?php echo $category->getId()?>_1">
						    	<?php 
						    		$selection_count = 0;
						    		singleGenealogy($root_level,1,$levelIds, $selection_count);
						    		
						    	?>
						    	</ul>
					    	</li>
						</ul>
				    </div>
				    <?php if ($selection_count > 0){
				    	?>
					    <div style="float:right; padding:5px;clear:both;">
					    
						    <?php
							$inclusive_categories_array = array();
							foreach ($inclusive_categories as $inclusive_category){
								array_push($inclusive_categories_array, $inclusive_category);
							}
							
						    ?>
						    <?php $inclusive_checked = in_array($category->getId(), $inclusive_categories_array) ? "checked" : "" ?>
						    <input <?php echo $inclusive_checked?> type="checkbox" name="chb_inclusive_<?php echo $category->getId()?>" value="chb_<?php echo $category->getId()?>"> Inclusive?
					    </div>
				    <?php 
				    }?>
				    
				    <input type="hidden" name="categories[]" value="<?php echo  $category->getId() ?>">
				    <?php }?>		       		
		       		</div>
		       		
		       		
     				<?php 
       				if ($i % 3 == 0 ){
       					?>
       					<div style="clear: both;"></div>
       					<?php 
       				}?>
		       		<?php 
		       }?>         		


     </div>
     
    
<script>

<?php if ($sf_user->hasFlash('saved_flash')){
	 ?>
	 $('#notification').show();
	 <?php 
	 }?>

<?php foreach ($categories as $category) { ?>
$("#tree_<?php echo $category->getId()?>")
	.jstree({ "plugins" : ["themes","html_data","ui"]
	 })

<?php }?>

function loadSearchConfiguration(){
	var php_url = '<?php echo url_for('search_configuration/loadSearchConfiguration?template_id=')?>';
	var template_id = document.getElementById('template_id').value;
	php_url = php_url.concat(template_id);	
	
	$.ajax({type: 'POST', url:php_url, script:true, cache:false, 
		success: function(data) { 
			 $('#templates_involved_levels').html(data);

			} });
	
}


	$('form').jqTransform({imgPath:'../images/forms'});
</script>      